// 下划转驼峰
export const camelCase = (str) => {
    return str.replace(/-[a-z]/g, (str1) => str1.substr(-1).toUpperCase());
};
// 手机号判断
export const judgePhone = (phone) => {
    phone = phone.toString().replace(/\s/g, ""); // 去掉字符串中的所有空格
    if (!isNaN(phone)) {
        // 以1开头的11位数字
        if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(phone)) {
            return false;
        } else {
            if (phone.toString().length !== 11) {
                return false;
            } else {
                return true;
            }
        }
    } else {
        return false;
    }
};
// URL截取
export const getUrlParam = (key, url) => {
    const reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
    const result = url
        ? url.split("?")[1].match(reg)
        : window.location.hash.split("?")[1]
            ? window.location.hash.split("?")[1].match(reg)
            : undefined;
    return result ? decodeURIComponent(result[2]) : undefined;
};
// 时间格式转换  把2019-11-06T09:24:24.630Z格式数据转化为 2019-11-06 17:24:24
export const resolvingDate = (date) => {
    const d = new Date(date);
    const month =
        d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
    const day = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
    const hours = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
    const min = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
    const sec = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
    const times =
        d.getFullYear() +
        "-" +
        month +
        "-" +
        day +
        " " +
        hours +
        ":" +
        min +
        ":" +
        sec;
    return times;
};
// 是否微信浏览器
export function isWeiXin() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    } else {
        return false;
    }
}
// 断是android还是ios还是web
export function isDevice() {
    const u = navigator.userAgent.toLowerCase();
    const isAndroid = u.indexOf("Android") > -1 || u.indexOf("Linux") > -1; // g
    const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios终端
    if (isAndroid) {
        // 这个是安卓操作系统
        return "isAndroid";
    }
    if (isIOS) {
        // 这个是ios操作系统
        return "isIOS";
    }
}

// 判断当前页面是否在webview里面打开(返回结果为true的时候代表当前是app中打开)
export function openInWebview() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        // 微信浏览器判断
        return false;
    } else if (ua.match(/QQ/i) == "qq") {
        // QQ浏览器判断
        return false;
    } else if (ua.match(/WeiBo/i) == "weibo") {
        return false;
    } else {
        if (ua.match(/Android/i) != null) {
            return ua.match(/browser/i) == null;
        } else if (ua.match(/iPhone/i) != null) {
            return ua.match(/safari/i) == null;
        } else {
            return ua.match(/macintosh/i) == null && ua.match(/windows/i) == null;
        }
    }
}
//函数防抖
export function debounce(func, wait, immediate) {
    //函数防抖[func 函数,wait 延迟执行毫秒数,immediate true 表立即执行,false 表非立即执行,立即执行是触发事件后函数会立即执行，然后n秒内不触发事件才能继续执行函数的效果]
    let timeout;
    return function () {
        let context = this;
        let args = arguments;
        if (timeout) clearTimeout(timeout);
        if (immediate) {
            var callNow = !timeout;
            timeout = setTimeout(() => {
                timeout = null;
            }, wait);
            if (callNow) func.apply(context, args);
        } else {
            timeout = setTimeout(function () {
                func.apply(context, args);
            }, wait);
        }
    };
}
// 函数节流
export function throttle(func, wait, type) {
    //函数节流 [func 函数 wait 延迟执行毫秒数 type 1 表时间戳版，2 表定时器版]
    let previous, timeout;
    if (type === 1) {
        previous = 0;
    } else if (type === 2) {
        timeout;
    }
    return function () {
        let context = this;
        let args = arguments;
        if (type === 1) {
            let now = Date.now();
            if (now - previous > wait) {
                func.apply(context, args);
                previous = now;
            }
        } else if (type === 2) {
            if (!timeout) {
                timeout = setTimeout(() => {
                    timeout = null;
                    func.apply(context, args);
                }, wait);
            }
        }
    };
}
// 将阿拉伯数字翻译成中文的数字
export function numberToChinese(num) {
    let AA = new Array(
        "零",
        "一",
        "二",
        "三",
        "四",
        "五",
        "六",
        "七",
        "八",
        "九",
        "十"
    );
    let BB = new Array("", "十", "百", "仟", "萬", "億", "点", "");
    let a = ("" + num).replace(/(^0*)/g, "").split(".");
    let k = 0;
    let re = "";
    for (let i = a[0].length - 1; i >= 0; i--) {
        switch (k) {
            case 0:
                re = BB[7] + re;
                break;
            case 4:
                if (!new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$").test(a[0])) {
                    re = BB[4] + re;
                }
                break;
            case 8:
                re = BB[5] + re;
                BB[7] = BB[5];
                k = 0;
                break;
        }
        if (k % 4 === 2 && a[0].charAt(i + 2) !== 0 && a[0].charAt(i + 1) === 0) {
            re = AA[0] + re;
        }
        if (a[0].charAt(i) !== 0) {
            re = AA[a[0].charAt(i)] + BB[k % 4] + re;
        }
        k++;
    }
    if (a.length > 1) {
        // 加上小数部分(如果有小数部分)
        re += BB[6];
        for (let i = 0; i < a[1].length; i++) {
            re += AA[a[1].charAt(i)];
        }
    }
    if (re === "一十") {
        re = "十";
    }
    if (re.match(/^一/) && re.length === 3) {
        re = re.replace("一", "");
    }
    return re;
}
// base64图片下载功能
export function downloadFile(base64, fileName) {
    let base64ToBlob = function (code) {
        let parts = code.split(";base64,");
        let contentType = parts[0].split(":")[1];
        let raw = window.atob(parts[1]);
        let rawLength = raw.length;
        let uInt8Array = new Uint8Array(rawLength);
        for (let i = 0; i < rawLength; ++i) {
            uInt8Array[i] = raw.charCodeAt(i);
        }
        return new Blob([uInt8Array], {
            type: contentType,
        });
    };
    let aLink = document.createElement("a");
    let blob = base64ToBlob(base64); //new Blob([content]);
    let evt = document.createEvent("HTMLEvents");
    evt.initEvent("click", true, true); //initEvent不加后两个参数在FF下会报错  事件类型，是否冒泡，是否阻止浏览器的默认行为
    aLink.download = fileName;
    aLink.href = URL.createObjectURL(blob);
    aLink.click();
}
//判断浏览器是否支持webP格式图片
export function isSupportWebP() {
    return (
        !![].map &&
        document
            .createElement("canvas")
            .toDataURL("image/webp")
            .indexOf("data:image/webp") == 0
    );
}